//
//  LoadingTipsView.m
//  TAOVideo
//
//  Created by Lin Eidan on 13-8-10.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "LoadingTipsView.h"
#import "Header.h"

#define ScrollViewShouldScrllOffsetY        (self.isGetDataImmediately ? 0 : 30)
#define CanUnClasp                          @"可以松开了"
#define CanUnClaspImageName                 @""

#define HardLoading                         @"努力加载中"

#define CanLoadingMore                      @"上拉加载更多"
#define CanLoadingMoreImageName             @""

@implementation LoadingTipsView

-(id)init{
    self = [super init];
    if (self) {
        self.isGetDataImmediately = YES;
        [[NSBundle mainBundle] loadNibNamed:@"LoadingTipsView" owner:self options:nil];
        self.width=self.customView.width;
        self.height=self.customView.height;
        [self addSubview:self.customView];
        if ([Common isIPad]) {
            self.loadingActView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
            self.loadingLabel.textColor = RGB(150, 150, 150);
        }
    }
    return self;
}

//获得总的数量后，调用，一般是在获得第一页数据后调用
-(void)drawNowInParentView:(UIScrollView *)scrollView andTotalPage:(int )totalPage{
    self.hidden=NO;
    [self handleToDefault:TipsStateNormal];
    self.isHasAllLoadedComplete=NO;
    
    self.totalPage=totalPage;
    if (self.totalPage == 1) {
        self.isHasAllLoadedComplete=YES;
        self.hidden=YES;
        return;
    }
    self.hasLoadPageNums=1;
    self.parentView=scrollView;
    if (!self.superview) {
        [self.parentView addSubview:self];
    }
    self.lastTop=0;
    self.top=self.parentView.contentHeight;
    self.left=(self.parentView.width-self.width)/2;
    
}

-(BOOL)isLoadingOrNot{
    if (self.state == TipsStateStartLoad) {
        return YES;
    }else{
        return NO;
    }
}

//正在滚动
-(void)onScroll{
    if (!self.isHasAllLoadedComplete) {
        float offsetY=self.parentView.contentOffsetTop+self.parentView.height-self.parentView.contentHeight;
        if (self.parentView.dragging || self.isGetDataImmediately) { //如果不立即获取数据，那么条件就只剩下是否被拖拽，如果立即获取数据，不管是否被拖拽，都触发。
            if (self.state != TipsStateStartLoad) {
                if (offsetY > ScrollViewShouldScrllOffsetY ) {
                    self.loadingLabel.text=CanUnClasp;
//                    self.loadingImageView.image=[UIImage imageNamed:CanUnClaspImageName];
                    self.state=TipsStateReadyLoad;
                    
                    //立即获取数据的话，这个时候就要去拉数据。
                    if (self.isGetDataImmediately) {
                        [self endDraing];
                    }
                }else {
                    [self handleToDefault:TipsStateNormal];
                }
            }
        }else{
            float offsetYTemp=self.top+self.height-self.parentView.height;
            if (self.state == TipsStateStartLoad && self.parentView.contentOffsetTop <= offsetYTemp) {
                //虽然下面有处理，犹豫惯性，其动画执行完后，还会继续往下走，这里处理以下，不让其往下走，停在这里。。
                self.parentView.contentOffsetTop=offsetYTemp;
            }
        }
        
    }
    
    if (self.state == TipsStateLoadedSuccess && !self.parentView.dragging) {
        self.parentView.contentOffsetTop=self.lastTop+self.height-self.parentView.height;
        self.state=TipsStateNormal;
    }

}

//获取数据成功
-(void)otherPageLoadSuccess{
    self.hasLoadPageNums++;
    self.lastTop=self.top;
    self.top=self.parentView.contentHeight;
    if (self.hasLoadPageNums == self.totalPage) {
        self.hidden=YES;
        self.isHasAllLoadedComplete=YES;
    }else{
        self.isHasAllLoadedComplete=NO;
        self.hidden=NO;
    }
    [self handleToDefault:TipsStateLoadedSuccess];
    
    if (self.isHasAllLoadedComplete) {  //防止拉到最后，其会导致gridView乱跳。 加载到最后一页了，也应该回到normal。。，但又要延迟一下，防止刚加载最后一页的时候乱跳。
        [self performSelector:@selector(delayResetTheState) withObject:nil afterDelay:0.3];
    }
}

-(void)delayResetTheState{
    self.state = TipsStateNormal;
}


//获取数据失败
-(void)otherPageLoadFailed{
    
    float delay=0.3;
    
    [self performSelector:@selector(handleToDefault:) withObject:nil afterDelay:delay inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
    [self performSelector:@selector(handleGetOtherPageDataFailed) withObject:nil afterDelay:delay inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}

////延迟处理界面一下,回到原位。
-(void)handleGetOtherPageDataFailed{
    
    float offsetY=self.top-self.parentView.height;
    [UIView animateWithDuration:0.4 animations:^{
        [self.parentView setContentOffset:CGPointMake(0,offsetY)];
    } completion:^(BOOL finished) {
       
        self.state=TipsStateNormal;
    }];
}

//停止拖动
-(void)endDraing{
   
    if (!self.isHasAllLoadedComplete) {
        float offsetYTemp=self.top+self.height-self.parentView.height;
        if (self.state == TipsStateReadyLoad) {
            self.state=TipsStateStartLoad;
            self.loadingImageView.hidden=YES;
            self.loadingActView.hidden=NO;
            self.loadingLabel.text=HardLoading;
            
            [UIView animateWithDuration:0.4 animations:^{
                [self.parentView setContentOffset:CGPointMake(0, offsetYTemp)];  //以动画的方式到那个位置，，但是到这这个位置后，其还会自动到底，所以上面还要处理。。
            } completion:^(BOOL finished) {
                
                if ([self.delegate respondsToSelector:@selector(loadingTipsView:startLoadThePage:)]) { //发新的请求
                    [self.delegate loadingTipsView:self startLoadThePage:self.hasLoadPageNums+1];
                }
            }];
        }
    }
    
}

//loading相关回到初始状态
-(void)handleToDefault:(LoadingTipsState)state{
    self.state=state;
    self.loadingLabel.text=CanLoadingMore;
//    self.loadingImageView.image=[UIImage imageNamed:CanLoadingMoreImageName];
    self.loadingActView.hidden=YES;
    self.loadingImageView.hidden=NO;
}

@end
